package com.xiaoxu.flow;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class FlowApp {


    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();

        FileSystem fileSystem = FileSystem.get(configuration);

        Path outPath = new Path("data\\output\\flow");

        if (fileSystem.exists(outPath)){
            fileSystem.delete(outPath,true);
            System.out.println("输出文件被删除");
        }

        Job job = Job.getInstance(configuration);

        //设置任务类
        job.setJarByClass(FlowApp.class);

        //设置map相关
        job.setMapperClass(FlowMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowEntity.class);

        //设置reduce相关
        job.setReducerClass(FlowReducer.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(FlowEntity.class);


        job.setPartitionerClass(FlowPartitioner.class);
        job.setNumReduceTasks(3);

        //设置输入输出路径
        FileInputFormat.setInputPaths(job,new Path("data\\input\\access.log"));
        FileOutputFormat.setOutputPath(job,outPath);

        job.waitForCompletion(true);

    }

}
